import webbrowser
import os

#python -u "D:\yurianime\YuriAnimeLinks.py"

files = os.listdir("D:\\yurianime\\SearchResults\\")
f2 = open("D:\\yurianime\\YuriAnimeResources.txt","w",encoding="utf-8")
f5 = open("D:\\yurianime\\YuriAnimeResources2.txt","w",encoding="utf-8")
f3 = open("D:\\yurianime\\YuriAnimeResourcesbyEpisodes.txt","w",encoding="utf-8")
f4 = open("D:\\yurianime\\YuriAnimeResourcesLinks.txt","w",encoding="utf-8")
for file in files:
    f1 = open("D:\\yurianime\\SearchResults\\"+file,"r",encoding="utf-8")
    lines = f1.readlines()
    f1.close()
    index = 0
    while index < len(lines) - 1:
        if "<title>" in lines[index]:
            f2.write("\n"+lines[index][15:lines[index].find("&quot;")]+"\n")
            f3.write("\n"+lines[index][15:lines[index].find("&quot;")]+"\n")
            f4.write("\n"+lines[index][15:lines[index].find("&quot;")]+"\n")
            f5.write("\n"+lines[index][15:lines[index].find("&quot;")]+"\n")
            index += 1
            continue
        elif "show-" in lines[index]:
            if "动画" in lines[index-2]:
                resource = lines[index+1][8:lines[index+1].rfind("</a>")]
                resource = resource.replace("<span class=\"keyword\">","")
                resource = resource.replace("</span>","").strip("\n")
                link = lines[index][lines[index].find("show-"):lines[index].rfind(".html")+5]
                link = "http://miobt.com/"+link
                check = True
                for i in range(60):
                    if ("["+str(i).zfill(2)+"]" in resource) or ("【"+str(i).zfill(2)+"】" in resource):
                        check = False
                        continue
                    if ("- "+str(i).zfill(2) in resource and "1-1" not in resource) or ("~ "+str(i).zfill(2) in resource and "1~1" not in resource):
                        check = False
                        continue
                    if ("第"+str(i).zfill(2)+"话" in resource) or ("第"+str(i).zfill(2)+"話" in resource) or ("第"+str(i).zfill(2)+"集" in resource):
                        check = False
                        continue
                    if (str(i)+"-1" in resource and "1-1" not in resource and "00-1" not in resource) or (str(i)+"~1" in resource and "1~1" not in resource and "00~1" not in resource):
                        check = False
                        continue
                    if " "+str(i).zfill(2)+" " in resource:
                        check = False
                        continue
                if ("第" in resource and "话" in resource and "-" not in resource and "~" not in resource) or ("第" in resource and "話" in resource and "-" not in resource and "~" not in resource) or ("第" in resource and "集" in resource and "-" not in resource and "~" not in resource):
                    check = False
                if ("-0" in resource and "show-0" not in resource) or ("~0" in resource):
                    check = False
                if check == True:
                    f2.write(resource)
                    f2.write(link+"\n")
                    f4.write(link+"\n")
                    f5.write(resource+"\n")
                    f5.write(link+"\n")
                elif check == False:
                    f3.write(resource)
                    f3.write(link+"\n")
            index += 1
            continue
        else:
            index += 1
            continue
    continue
f2.close()
f3.close()
f4.close()